package idv.loveloser.servlet;

import idv.loveloser.dao.MdlContext;
import idv.loveloser.dao.MdlContextDAO;
import idv.loveloser.dao.MdlCourseModules;
import idv.loveloser.dao.MdlCourseModulesDAO;
import idv.loveloser.dao.MdlCourseSections;
import idv.loveloser.dao.MdlCourseSectionsDAO;
import idv.loveloser.dao.MdlForum;
import idv.loveloser.dao.MdlForumDAO;
import idv.loveloser.util.Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class AddCourseForum extends HttpServlet {
	private MdlCourseModulesDAO courseModuleDAO = new MdlCourseModulesDAO();
	private MdlContextDAO contextDAO = new MdlContextDAO();
	private MdlForumDAO forumDAO =new MdlForumDAO(); 
	private MdlCourseSectionsDAO sectionsDAO = new MdlCourseSectionsDAO();
	
	/**
	 * Constructor of the object.
	 */
	public AddCourseForum() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
		String sectionId=request.getParameter("sectionId");
		String courseId = request.getParameter("courseId");
		String forumName = request.getParameter("forum_name");
		String forumDescription=request.getParameter("forum_description");
		String contextPath="";
		MdlForum forum = new MdlForum();
		MdlCourseSections section = new MdlCourseSections();
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
//		if(Util.isAuthorised(client, sessionKey)){
			MdlCourseModules modules = new MdlCourseModules();
			modules.setAdded(System.currentTimeMillis());
			modules.setAvailablefrom(0L);
			modules.setAvailableuntil(0L);
			modules.setCompletion(false);
			modules.setCompletionexpected(0L);
			modules.setCompletiongradeitemnumber(null);
			modules.setCompletionview(false);
			modules.setCourse(Long.parseLong(courseId));
			modules.setGroupingid(0L);
			modules.setGroupmembersonly((short)0);
			modules.setGroupmode((short)0);
			modules.setIdnumber("");
			modules.setIndent(0);
			modules.setInstance(0L);
			modules.setModule(7L);
			modules.setScore((short)0);
			if(!sectionId.equals("-1"))
				modules.setSection(Long.parseLong(sectionId));
			else
				modules.setSection(0L);
			modules.setShowavailability(true);
			modules.setShowdescription(false);
			modules.setVisible(true);
			modules.setVisibleold(true);
			courseModuleDAO.save(modules);
			forum.setAssessed(0L);
			forum.setAssesstimefinish(0L);
			forum.setAssesstimestart(0L);
			forum.setBlockafter(0L);
			forum.setBlockperiod(0L);
			forum.setCompletiondiscussions(0);
			forum.setCompletionposts(0);
			forum.setCompletionreplies(0);
			forum.setCourse(Long.parseLong(courseId));
			forum.setForcesubscribe(false);
			forum.setIntro(forumDescription);
			forum.setIntroformat((short)1);
			forum.setMaxattachments(9L);
			forum.setMaxbytes(512000L);
			forum.setName(forumName);
			forum.setRssarticles((short)0);
			forum.setRsstype((short)0);
			forum.setScale(0L);
			forum.setTimemodified(System.currentTimeMillis());
			forum.setTrackingtype((short)1);
			forum.setType("general");
			forum.setWarnafter(0L);
			forumDAO.save(forum);
			modules.setInstance(forum.getId());
			courseModuleDAO.merge(modules);
			List<MdlContext> selectedContexts=contextDAO.findByContextlevel(50L);
			MdlContext courseContext =null;
			for(int index=0;index<selectedContexts.size();index++){
				MdlContext context=selectedContexts.get(index);
				if(context.getInstanceid()==Long.parseLong(courseId)){
					courseContext=context;
					break;
				}
			}
			MdlContext insertContext=new MdlContext();
			insertContext.setContextlevel(70L);
			insertContext.setDepth((short)(courseContext.getDepth()+1));
			insertContext.setPath("");
			insertContext.setInstanceid(modules.getId());
			
			contextDAO.save(insertContext);
			insertContext.setPath(courseContext.getPath()+"/"+insertContext.getId());
			contextDAO.merge(insertContext);
			if(sectionId.equals("-1")){
				section = new MdlCourseSections();
				section.setCourse(Long.parseLong(courseId));
				section.setName("");
				section.setSequence(forum.getId()+"");
				section.setSummary("");
				section.setSummaryformat((short)1);
				section.setVisible(true);
				section.setSection(0L);
				sectionsDAO.save(section);
			}
//			MdlCourseSections section=sectionsDAO.findById(Long.parseLong(sectionId));
			
			if(!sectionId.equals("-1")){
				String sequence = section.getSequence()+","+modules.getId();
				section.setSequence(sequence);
				sectionsDAO.merge(section);
			}else{
				section.setSequence(modules.getId()+"");
				sectionsDAO.merge(section);
			}
//		}
		Gson gson = new Gson();
		out.println(gson.toJson(forum));
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
